package com.woniu.gf.pawn.store.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.gf.pawn.comm.core.domain.Management;
import com.woniu.gf.pawn.comm.core.domain.Store;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.comm.core.exception.PawnExceptionCode;
import com.woniu.gf.pawn.management.client.ManagementClient;
import com.woniu.gf.pawn.management.client.RoleClient;
import com.woniu.gf.pawn.management.param.SelectManagementByRoleIdParam;
import com.woniu.gf.pawn.store.dto.SelectStoreByShopIdDto;
import com.woniu.gf.pawn.store.dto.SelectStoreDto;
import com.woniu.gf.pawn.store.dto.StoreDto;
import com.woniu.gf.pawn.store.param.AddStoreParam;
import com.woniu.gf.pawn.store.param.AlterStoreParam;
import com.woniu.gf.pawn.store.param.SelectStoreByShopIdParam;
import com.woniu.gf.pawn.store.param.SelectStoreParam;
import com.woniu.gf.pawn.store.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-16
 */
@RestController
@RequestMapping("store")
@Api(tags = "仓库")
@CrossOrigin
public class StoreController {
    @Autowired
    private RoleClient roleClient;
    @Autowired
    private ManagementClient managementClient;

    @Autowired
    private StoreService storeService;
    /**
    *@Description: 查询仓库管理员
    *@return:
    */
    @GetMapping("/selectStoreManagements")
    @ApiOperation("查询仓库管理员")
    public ResultDto selectStoreManagements(@Valid SelectManagementByRoleIdParam selectManagementByRoleIdParam){
        System.out.println(selectManagementByRoleIdParam);
        ResultDto resultDto = roleClient.selectManagementByRoleId(selectManagementByRoleIdParam);
        return resultDto;
    }
    /**
    *@Description: 新增门店
    *@return:
    */
    @PostMapping("/addStore")
    @ApiOperation("新增仓库")
    public ResultDto addStore(AddStoreParam addStoreParam){
        int managementId = addStoreParam.getManagementId();
        Management management = managementClient.selectManagementById(managementId);
        if(management ==null){
            ResultDto.fail(PawnExceptionCode.STORE_MANAGEMENT_NONENTITY.getCode(),PawnExceptionCode.STORE_MANAGEMENT_NONENTITY.getMsg());
        }
        Store store = new Store();
        BeanUtils.copyProperties(addStoreParam,store);
        store.setStoreStatus("启用");
        storeService.save(store);
        return ResultDto.success(200,"新增仓库成功");
    }
    /**
    *@Description: 修改仓库信息
    *@return:
    */
    @PutMapping("/alterStore")
    @ApiOperation("修改仓库信息")
    public ResultDto alterStore(@RequestBody @Valid AlterStoreParam alterStoreParam){
        int managementId = alterStoreParam.getManagementId();
        Management management = managementClient.selectManagementById(managementId);
        if(management ==null){
            ResultDto.fail(PawnExceptionCode.STORE_MANAGEMENT_NONENTITY.getCode(),PawnExceptionCode.STORE_MANAGEMENT_NONENTITY.getMsg());
        }
        int storeId = alterStoreParam.getStoreId();
        Store store = storeService.getById(storeId);
        if(store==null){
            ResultDto.fail(PawnExceptionCode.STORE_ID_NONENTITY.getCode(),PawnExceptionCode.STORE_ID_NONENTITY.getMsg());
        }
        BeanUtils.copyProperties(alterStoreParam,store);
        UpdateWrapper<Store> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("store_name",store.getStoreName());
        updateWrapper.set("manager_id",store.getManagerId());
        updateWrapper.set("store_addr",store.getStoreAddr());
        updateWrapper.set("store_status",store.getStoreStatus());
        updateWrapper.eq("store_id",store.getStoreId());
        storeService.update(updateWrapper);
        return ResultDto.success(200,"修改仓库信息成功");
    }
    /**
    *@Description: 删除仓库
    *@return:
    */
    @DeleteMapping("/deleteStore/{storeId}")
    @ApiOperation("删除仓库")
    public ResultDto deleteStore(@PathVariable int storeId){
        storeService.removeById(storeId);
        return ResultDto.success(200,"删除仓库成功");
    }
    /**
    *@Description: 分页查询仓库
    *@return:
    */
    @GetMapping("/selectAllStore")
    @ApiOperation("分页查询仓库")
    public ResultDto selectAllStore(@Valid SelectStoreParam selectStoreParam){
        IPage<SelectStoreDto> storeDtoIPage=storeService.selectPageStore(selectStoreParam);
        List<SelectStoreDto> selectStoreDtos = storeDtoIPage.getRecords();
        if(selectStoreDtos == null){
            return ResultDto.success(200,"查询仓库信息成功",storeDtoIPage);
        }
        for(SelectStoreDto selectStoreDto:selectStoreDtos){
            int storeId = selectStoreDto.getStoreId();
            List<String> shopList=storeService.selectShopByStoreId(storeId);
            selectStoreDto.setShopList(shopList);
        }
        return ResultDto.success(200,"查询仓库信息成功",storeDtoIPage);
    }
    /**
     *@Description: 查询所有已使用仓库
     *@return:
     */
    @GetMapping("/selectAllStores")
    @ApiOperation("查询所有已使用仓库")
    public ResultDto selectAllStores(){
        List<Store> stores=storeService.selectAllStores();

        return ResultDto.success(200,"查询仓库信息成功",stores);
    }
    /**
    *@Description: 根据门店id查询该门店拥有的仓库集合
    *@return:
    */
    @GetMapping("selectStoreByShopId")
    @ApiOperation("根据门店id查询该门店拥有的仓库集合")
    public ResultDto SelectStoreByShopId(@Valid SelectStoreByShopIdParam selectStoreByShopIdParam){
        SelectStoreByShopIdDto selectStoreByShopIdDto = new SelectStoreByShopIdDto();
        BeanUtils.copyProperties(selectStoreByShopIdParam,selectStoreByShopIdDto);
        List<Store> storeList=storeService.selectStoreByShopId(selectStoreByShopIdDto);
        return getResultDto(storeList);
    }
    /**
    *@Description: 根据门店id查询该门店未拥有的仓库集合
    *@return:
    */
    @GetMapping("selectStoreNotByShopId")
    @ApiOperation("根据门店id查询该门店未拥有的仓库集合")
    public ResultDto selectStoreNotByShopId(@Valid SelectStoreByShopIdParam selectStoreByShopIdParam){
        SelectStoreByShopIdDto selectStoreByShopIdDto = new SelectStoreByShopIdDto();
        BeanUtils.copyProperties(selectStoreByShopIdParam,selectStoreByShopIdDto);
        List<Store> storeList=storeService.selectStoreNotByShopId(selectStoreByShopIdDto);
        return getResultDto(storeList);
    }

    private ResultDto getResultDto(List<Store> storeList) {
        if(storeList ==null || storeList.get(0)==null || storeList.isEmpty()){
            return ResultDto.success(200,"查询成功");
        }
        List<StoreDto> storeDtoList=new ArrayList<>();
        for(Store store:storeList){
            StoreDto storeDto = new StoreDto();
            BeanUtils.copyProperties(store,storeDto);
            storeDtoList.add(storeDto);
        }
        return ResultDto.success(200,"查询成功",storeDtoList);
    }

}

